package com.example.spsaveqq;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_account;
    private EditText et_password;
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        // 将账号密码显示在界面上
        Map<String, String> userInfoMap = SPSaveQQ.getUserInfo(this);
        if (userInfoMap != null) {
            String account = userInfoMap.get("account");
            String password = userInfoMap.get("password");
            et_account.setText(account);
            et_password.setText(password);
        }
    }

    // 实现点击事件
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                // 如果点击了登录按钮, 则保存值到 data.xml 中
                String account = et_account.getText().toString().trim();
                String password = et_password.getText().toString();
                // 判空
                if (TextUtils.isEmpty(account)) {
                    Toast.makeText(this, "请输入QQ账号: ", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(this, "请输入QQ密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                // 将账号密码存储到 data.xml 文件中
                boolean isSaveSuccess = SPSaveQQ.saveUserInfo(this, account, password);
                if (isSaveSuccess) {
                    Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    // 初始化组件
    private void initView() {
        et_account = (EditText) findViewById(R.id.et_account);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_login = (Button) findViewById(R.id.btn_login);
        // 添加监听事件
        btn_login.setOnClickListener(this);
    }
}